<template>
        <div class="message_body">
            <h3>个人信息</h3>
            <!-- elementui表单 -->
            <el-form ref="form" :model="messageForm" label-width="80px" :disabled="disabled">
  <el-form-item label="昵称">
    <el-input v-model="messageForm.name"></el-input>
  </el-form-item>
  <el-form-item label="地区">
    <el-select v-model="messageForm.region" placeholder="请选择你所在城市">
      <el-option label="上海" value="shanghai"></el-option>
      <el-option label="北京" value="beijing"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item label="出生日期">
      <el-date-picker type="date" placeholder="选择日期" v-model="messageForm.birthday" style="width: 100%;"></el-date-picker>
  </el-form-item>
  <el-form-item label="手机">
    <el-input v-model="messageForm.phonenumber"></el-input>
  </el-form-item>
  <el-form-item label="邮箱">
    <el-input v-model="messageForm.email"></el-input>
  </el-form-item>
</el-form>
        <div class="message_btn">
        <el-button type="primary" @click="changemessageFlag" v-if="disabled">修改资料</el-button>
        <div v-else>
        <el-button type="primary" @click="changemessage">提交</el-button>
        <el-button @click="cancelChangemessage">取消</el-button>
        </div>
        </div>
</div>
</template>
<script>
export default {
    data() {
        return {
            messageForm:{
                name:"黄小明",
                region:"",
                birthday:"",
                phonenumber:"",
                email:'',
            },
            disabled:true,
            token:this.$store.state.token,
        }
    },
    methods: {
       changemessageFlag(){
         this.disabled=!this.disabled
       },
      changemessage(){
        var params = new URLSearchParams();
        params.append("name", this.messageForm.name);
        params.append("userid", this.$store.state.userid);
        params.append("region", this.messageForm.region);
        params.append("phone", this.messageForm.phonenumber);
        params.append("email", this.messageForm.email);
        this.$http.post('/client/client_detail/edit/finish',params).then( res => {
          // window.console.log(res)
          if(res.data.msg === '修改成功'){
          this.$message.success('修改成功')
          this.changemessageFlag()
          }else{
            this.$message.error('修改失败')
            this. getMessageInfo()
          }
        })
      },
      cancelChangemessage(){
        this. getMessageInfo()
        this.changemessageFlag()
      },
      getMessageInfo(){
      // 去除token中的斜杆方法
          var reg = new RegExp('-','gi');
          var result = this.token.replace(reg,'');
          this.token = result
        window.console.log(this.token)
        this.$http.get('/client/client_detail/'+ this.token).then(res=>{
          window.console.log(res)
          this.messageForm.name = res.data.name
          this.messageForm.region = res.data.area
          this.messageForm.birthday = res.data.birthday
          this.messageForm.phonenumber = res.data.phone
          this.messageForm.email = res.data.email
        })
      },
    },
    created(){
      this.getMessageInfo()
    }
}
</script>
<style scoped>
.message_body{
    
}
h3{
      text-align: center;
  }
.message_btn{
      float: right;
  }
</style>